package com.indigon.parkStory.control
{
	import com.indigon.parkStory.data.UserData;
	import com.indigon.utils.SoundManager;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class ControlMenu
	{
		private static const MENU_BUTTON_REL_POS      : Number = 35.0;
		private static const MENU_BUTTON_POS_DIFF_BIG : Number = 84.0;
		private static const MENU_BUTTON_POS_DIFF_SML : Number = 66.0;
		
		private static const PAUSE_LABEL     : String = "pause";
		private static const RESUME_LABEL    : String = "resume";
		private static const SOUND_ON_LABEL  : String = "on";
		private static const SOUND_OFF_LABEL : String = "off";
		
		private static const MODAL_COLOR : uint   = 0x000000;
		private static const MODAL_ALPHA : Number = 0.5;
		
		private var _menuBar       : Sprite;
		private var _menuButtons   : Sprite;
		private var _controlButton : MovieClip;
		private var _soundButton   : MovieClip;
		private var _helpButton    : MovieClip;
		private var _replayButton  : MovieClip;
		private var _endButton     : MovieClip;
		
		private var _control : ControlMain;
		
		public var graphic : Sprite;
		
		public function ControlMenu(control:ControlMain, width:Number, height:Number)
		{
			this.graphic  = new Sprite();
			this._control = control;
			
			_controlButton = new GFX_ButtonUI();
			_controlButton.x = MENU_BUTTON_REL_POS;
			_controlButton.y = MENU_BUTTON_REL_POS;
			_controlButton.stop();
			graphic.addChild(_controlButton);
			
			_menuBar = new GFX_ControlUI();
			_menuBar.height = height;
			
			_menuButtons = new Sprite();
			
			_menuBar.graphics.beginFill(MODAL_COLOR, MODAL_ALPHA);
			_menuBar.graphics.drawRect(graphic.width, 0.0, width - graphic.width, height);
			_menuBar.graphics.endFill();

			_soundButton = new GFX_SoundButton();
			_soundButton.x = MENU_BUTTON_REL_POS;
			_soundButton.y = height - MENU_BUTTON_REL_POS;
			_soundButton.stop();
			_menuButtons.addChild(_soundButton);
			
			_helpButton = new GFX_HelpButton();
			_helpButton.x = MENU_BUTTON_REL_POS;
			_helpButton.y = height - MENU_BUTTON_REL_POS - MENU_BUTTON_POS_DIFF_SML;
			_helpButton.stop();
			_menuButtons.addChild(_helpButton);
			
			_replayButton = new GFX_ReplayButton();
			_replayButton.x = MENU_BUTTON_REL_POS;
			_replayButton.y = MENU_BUTTON_REL_POS + MENU_BUTTON_POS_DIFF_BIG;
			_replayButton.stop();
			_menuButtons.addChild(_replayButton);
			
			_endButton = new GFX_IndexButton();
			_endButton.x = MENU_BUTTON_REL_POS;
			_endButton.y = MENU_BUTTON_REL_POS + (2 * MENU_BUTTON_POS_DIFF_BIG);
			_endButton.stop();
			_menuButtons.addChild(_endButton);
		}
		
		public function set(container:DisplayObjectContainer, x:Number=0, y:Number=0, layer:uint=0):void
		{
			graphic.x = x;
			graphic.y = y;
			container.addChildAt(graphic, layer);
		}
		
		public function reset():void
		{
			_controlButton.gotoAndStop(PAUSE_LABEL);
		}
		
		public function start():void
		{
			graphic.visible = true;
			_controlButton.addEventListener(MouseEvent.MOUSE_DOWN, pressControl);
		}
		
		public function end():void
		{
			_controlButton.removeEventListener(MouseEvent.MOUSE_DOWN, pressControl);
		}
		
		private function removeMenu():void
		{
			_menuButtons.parent.removeChild(_menuButtons);
			_menuBar.parent.removeChild(_menuBar);
			
			_soundButton.removeEventListener(MouseEvent.MOUSE_DOWN, pressSound);
			_helpButton.removeEventListener(MouseEvent.MOUSE_DOWN, pressHelp);
			_replayButton.removeEventListener(MouseEvent.MOUSE_DOWN, pressReplay);
			_endButton.removeEventListener(MouseEvent.MOUSE_DOWN, pressEnd);
		}
		
		private function showMenu():void
		{
			graphic.addChildAt(_menuBar, 0);
			graphic.addChildAt(_menuButtons, 1);
			
			_soundButton.addEventListener(MouseEvent.MOUSE_DOWN, pressSound);
			_helpButton.addEventListener(MouseEvent.MOUSE_DOWN, pressHelp);
			_replayButton.addEventListener(MouseEvent.MOUSE_DOWN, pressReplay);
			_endButton.addEventListener(MouseEvent.MOUSE_DOWN, pressEnd);
		}
		
		private function pressReplay(e:Event):void
		{
			SoundManager.instance.playSoundByClass(SFX_UI);
			removeMenu();
			_control.restartSession();
			e.stopImmediatePropagation();
		}
		
		private function pressEnd(e:Event):void
		{
			SoundManager.instance.playSoundByClass(SFX_UI);
			// TODO
			e.stopImmediatePropagation();
		}
		
		private function pressHelp(e:Event):void
		{
			SoundManager.instance.playSoundByClass(SFX_UI);
			// TODO
			e.stopImmediatePropagation();
		}
		
		private function pressSound(e:Event):void
		{
			UserData.instance.soundOn = !UserData.instance.soundOn;
			SoundManager.instance.setGlobalSound(UserData.instance.soundOn);
			
			if (UserData.instance.soundOn)
			{
				_soundButton.gotoAndStop(SOUND_ON_LABEL);
				SoundManager.instance.playSoundByClass(SFX_UI);
			}
			else
			{
				_soundButton.gotoAndStop(SOUND_OFF_LABEL);
			}
			e.stopImmediatePropagation();
		}
		
		private function pressControl(e:MouseEvent=null):void
		{
			SoundManager.instance.playSoundByClass(SFX_UI);
			
			if (_control.pauseFlag)
			{
				_control.resume();
				removeMenu();
				_controlButton.gotoAndStop(PAUSE_LABEL);
			}
			else
			{
				_control.pause();
				showMenu();
				_controlButton.gotoAndStop(RESUME_LABEL);
			}
			e.stopImmediatePropagation();
		}		
	}
}